package com.journaldev.spring.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import com.journaldev.spring.model.Medicine;
import com.journaldev.spring.model.Prescription;
import com.journaldev.spring.model.PrescriptionMedicine;
import com.journaldev.spring.model.PrescriptionMedicineId;

@Transactional
public class PrescriptionMedicineDAOImpl implements PrescriptionMedicineDAO {
	
	private static final Logger logger = LoggerFactory.getLogger(CityDAOImpl.class);

	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sf){
		this.sessionFactory = sf;
	}

	@Override
	public void removePrescriptionMedicine(Prescription oresc, Medicine med) {
		// TODO Auto-generated method stub
		Session session = this.sessionFactory.getCurrentSession();
		PrescriptionMedicine p = new PrescriptionMedicine();
		PrescriptionMedicineId pk = new PrescriptionMedicineId();
		pk.setMedicine(med);
		pk.setPrescription(oresc);
		p.setPk(pk);
		
		if(null != p){
			session.delete(p);
		}
		logger.info("PrescriptionMedicine deleted successfully, PrescriptionMedicine details="+p);
	}

}
